#include "queue.h"

struct _queue_ {
	void **data;
	int w_cursor;
	int r_cursor;
	int len;
};
queue_s * queue_create(int size)
{
	queue_s *queue = (queue_s *)malloc(sizeof(queue_s));

	queue->w_cursor = 0;
	queue->r_cursor = 0;
	if (queue != NULL && size > 0) {
		queue->data = (void  **)malloc(size*sizeof(void *));
		if (queue->data != NULL) {
			queue->len = size;
		}
	}
	return queue;
}
void  queue_destory(queue_s * queue)
{
	if (queue != NULL) {
		if (queue->data != NULL) {
			free(queue->data);
		}
		free(queue);
	}
}


ret_e queue_in(queue_s *queue,void * data)
{
	int w_cursor;
	if (queue == NULL) {
		return RET_PRA;
	}
	
	if (queue->data == NULL) {
		return RET_MEM;
	}
	w_cursor = ((queue->w_cursor+1) % queue->len);
	if (queue->r_cursor != w_cursor) {
		queue->data[queue->w_cursor] = data;
		queue->w_cursor = w_cursor;
		return RET_OK	;
	}
	return RET_FULL;
}

ret_e queue_out(queue_s *queue,void ** data)
{
	if (queue == NULL) {
		return RET_PRA;
	}
	if (queue->w_cursor == queue->r_cursor){
		return RET_END;
	}
	if (data != NULL) {
		*data  = queue->data[queue->r_cursor];
		queue->r_cursor = (queue->r_cursor+1) % queue->len;
	}
	return RET_OK;
}

ret_e queue_is_empty(queue_s * queue)
{
	if (queue->w_cursor == queue->r_cursor){
		return RET_END;
	}
	return RET_OK;
}


